<?php
/**
 * Created by PhpStorm.
 * User: yuguozhen
 * Date: 16/8/1
 * Time: 下午6:32
 */
namespace models\Timer;
use library\DataLayer;

class TimersUpModel extends DataLayer{

    /**
     * 重新统计特定的某一天
     */
    public function TimerUpDay($now_day){

        $HOSPITAL = $this->TimersUp($now_day,'HOSPITAL',1);
        $WEB_ACCOUNT = $this->TimersUp($now_day,'WEB_ACCOUNT',1);
        $INNER_WEB_ACCOUNT = $this->TimersUp($now_day,'INNER_WEB_ACCOUNT',1);
        $EXTERNAL_WEB_ACCOUNT = $this->TimersUp($now_day,'EXTERNAL_WEB_ACCOUNT',1);
        $GUIDANCE_CLASS = $this->TimersUp($now_day,'GUIDANCE_CLASS',1);
        $EXPERT_CLASS = $this->TimersUp($now_day,'EXPERT_CLASS',1);
        $NURSE_PUSH_CLASS = $this->TimersUp($now_day,'NURSE_PUSH_CLASS',1);
        $READ_PUSH_CLASS = $this->TimersUp($now_day,'READ_PUSH_CLASS',1);
        $QUESTION_PUSH_CLASS = $this->TimersUp($now_day,'QUESTION_PUSH_CLASS',1);

        if($HOSPITAL&&$WEB_ACCOUNT&&$INNER_WEB_ACCOUNT&&$EXTERNAL_WEB_ACCOUNT&&$GUIDANCE_CLASS&&$EXPERT_CLASS&&$NURSE_PUSH_CLASS&&$READ_PUSH_CLASS&&$QUESTION_PUSH_CLASS){
            return true;

        }else{
            return false;
        }

    }
    /**
     * 重新统计特定的某一天的某一个
     * @param $now_day 统计的特定时间
     * @param string $name 统计名
     * @param int $type 统计类型1-增加,2-总共
     * @return mixed
     */
    public function TimersUp($now_day,$name='HOSPITAL',$type=1){

        $now_start = $now_day." 00:00:00";
        $now_end = $now_day." 23:59:59";

        //是否记录过
        $Mstatistics = new \models\Timer\Count\StatisticsModel();
        $ifrecode = $Mstatistics->ifRecord($name,$now_start,$now_end,$type);

        $vaule = $this->getVaule($name,$now_start,$now_end,$type);

        if($ifrecode){
            $Mrecord = new \models\Timer\One\StatisticsModel();
            $recordidArr = $Mrecord->findOne($name,$now_start,$now_end,$type);

            $Mrecord = new \models\Timer\Update\StatisticsModel();
            $up_res = $Mrecord->toUpdate($recordidArr);
        }

        if( $ifrecode && $up_res<=0  ){
            $ret['status'] = 'err';
            $ret['errcode'] = 'update_fail';
        }else{
            //数据统计
            $Mnew_statistics = new \models\Timer\Insert\StatisticsModel();
            $insert = $Mnew_statistics->toInsert($now_start,$vaule,$name,$type);
            if($insert){
                $ret['status'] = 'ok';
                $ret['msg'] = 'update_success';
            }
        }
        return $ret;
    }

    public function getVaule($name,$now_start,$now_end,$type){

        switch($name){
            case 'HOSPITAL':
                $Mhospital = new \models\Timer\Count\HospitalModel();
                if($type==1){
                    $vaule = $Mhospital->NEW_HOSPITAL($now_start,$now_end);
                }elseif($type==2){
                    $vaule = $Mhospital->ALL_HOSPITAL($now_end);
                }
                break;
            case 'WEB_ACCOUNT':
                $Mweb = new \models\Timer\Count\WebAccountModel();
                $vaule = $Mweb->WEB_ACCOUNT($now_start,$now_end);
                break;
            case 'INNER_WEB_ACCOUNT':
                $Mweb = new \models\Timer\Count\WebAccountModel();
                $vaule = $Mweb->INNER_WEB_ACCOUNT($now_start,$now_end);
                break;
            case 'EXTERNAL_WEB_ACCOUNT':
                $Mweb = new \models\Timer\Count\WebAccountModel();
                $vaule = $Mweb->EXTERNAL_WEB_ACCOUNT($now_start,$now_end);
                break;
            case 'GUIDANCE_CLASS':
                $Mweb = new \models\Timer\Count\GuidanceClassModel();
                $vaule = $Mweb->GUIDANCE_CLASS($now_start,$now_end);
                break;
            case 'EXPERT_CLASS':
                $Mweb = new \models\Timer\Count\GuidanceClassPublicModel();
                $vaule = $Mweb->EXPERT_CLASS($now_start,$now_end);
                break;
            case 'NURSE_PUSH_CLASS':
                $Mweb = new \models\Timer\Count\NursePushClassModel();
                $vaule = $Mweb->NURSE_PUSH_CLASS($now_start,$now_end);
                break;
            case 'READ_PUSH_CLASS':
                $Mweb = new \models\Timer\Count\PatientInhospitalReadingDetailModel();
                $vaule = $Mweb->READ_PUSH_CLASS($now_start,$now_end);
                break;
            case 'QUESTION_PUSH_CLASS';
                $Mweb = new \models\Timer\Count\PatientInhospitalReadingDetailModel();
                $vaule = $Mweb->QUESTION_PUSH_CLASS($now_start,$now_end);
                break;
        }
        return $vaule;
    }


}